package org.roulette.model;

import java.util.ArrayList;
import javax.xml.bind.annotation.*;
import org.roulette.model.Table;

@XmlRootElement(name = "table")
@XmlAccessorType(XmlAccessType.FIELD)
public class Table {
	private int id;
	private ArrayList<Player> players;
	private ArrayList<Field> fields;
	private int result;
	private int counter;
	
	public int getCounter() {
		return counter;
	}

	public void setCounter(int counter) {
		this.counter = counter;
	}

	public Table() {
		players = new ArrayList<Player>();
		fields = new ArrayList<Field>();
		init();
	}

	public Table(int id) {
		this();
		this.id = id;
	}

	public void init() {
		fields.add(new Field(0, "green"));
		fields.add(new Field(1, "red"));
		fields.add(new Field(2, "black"));
		fields.add(new Field(3, "red"));
		fields.add(new Field(4, "black"));
		fields.add(new Field(5, "red"));
		fields.add(new Field(6, "black"));
		fields.add(new Field(7, "red"));
		fields.add(new Field(8, "black"));
		fields.add(new Field(9, "red"));
		fields.add(new Field(10, "black"));
		fields.add(new Field(11, "black"));
		fields.add(new Field(12, "red"));
		fields.add(new Field(13, "black"));
		fields.add(new Field(14, "red"));
		fields.add(new Field(15, "black"));
		fields.add(new Field(16, "red"));
		fields.add(new Field(17, "black"));
		fields.add(new Field(18, "red"));
		fields.add(new Field(19, "red"));
		fields.add(new Field(20, "black"));
		fields.add(new Field(21, "red"));
		fields.add(new Field(22, "black"));
		fields.add(new Field(23, "red"));
		fields.add(new Field(24, "black"));
		fields.add(new Field(25, "red"));
		fields.add(new Field(26, "black"));
		fields.add(new Field(27, "red"));
		fields.add(new Field(28, "black"));
		fields.add(new Field(29, "black"));
		fields.add(new Field(30, "red"));
		fields.add(new Field(31, "black"));
		fields.add(new Field(32, "red"));
		fields.add(new Field(33, "black"));
		fields.add(new Field(34, "red"));
		fields.add(new Field(35, "black"));
		fields.add(new Field(36, "red"));
		fields.add(new Field(37, "eersteTwaalf"));
		fields.add(new Field(38, "tweedeTwaalf"));
		fields.add(new Field(39, "derdeTwaalf"));
		fields.add(new Field(40, "linkerRij"));
		fields.add(new Field(41, "middenRij"));
		fields.add(new Field(42, "rechterRij"));
		fields.add(new Field(43, "even"));
		fields.add(new Field(44, "oneven"));
		fields.add(new Field(45, "theRed"));
		fields.add(new Field(46, "theBlack"));
		fields.add(new Field(47, "eersteAchttien"));
		fields.add(new Field(48, "tweedeAchttien"));
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ArrayList<Player> getPlayerList() {
		return players;
	}

	public ArrayList<Field> getBetOptions() {
		return fields;
	}

	public void setPlayerList(ArrayList<Player> playerList) {
		this.players = playerList;
	}

	public void setBetOptions(ArrayList<Field> betOptions) {
		this.fields = betOptions;
	}
	
	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public void addPlayerToTable(Player player) {
		if (!players.contains(player)) {
			Game.removePlayerFromTable(player.getName());
			players.add(player);
		}
	}

	public void removePlayerFromTable(Player player) {
		if (players.contains(player)) {
			System.out.println(player.getName()
					+ " has been removed from table " + getId());
			players.remove(player);
		}
	}

	public Player getPlayerByName(String name) {
		Player player = null;
		for (Player p : players) {
			if (p.getName().equals(name)) {
				player = p;
			}
		}
		return player;
	}

	public Field getBetField(int i) {
		return fields.get(i);
	}

	public void setBet(Player player, Field field,  Bet bet) {
		boolean isValid = false;
		for (Field f : fields) {
			if (f.equals(field)) {
				isValid = true;
			}
		}
		if (isValid) {
			field.addBet(bet);
		}
	}
	
}
